package guda.house.biz.impl;

import java.util.ArrayList;
import java.util.List;

import guda.house.biz.beans.vo.SuiteView;
import guda.house.biz.constants.CommonResultCode;
import guda.house.biz.query.WxSuiteRelQuery;
import guda.house.biz.security.AuthUser;
import guda.house.dal.dao.*;
import guda.house.dal.dataobject.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.house.biz.WxAccessBiz;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;
import org.springframework.util.StringUtils;

public class WxAccessBizImpl implements WxAccessBiz{

    private final static Logger logger = LoggerFactory.getLogger(WxAccessBizImpl.class);

    @Autowired
    private WxAccessDAO wxAccessDAO;
    @Autowired
    private WxSuiteRelDAO wxSuiteRelDAO;
    @Autowired
    private SuiteDAO suiteDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private VillageDAO villageDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            WxAccessDO wxAccessDO = wxAccessDAO.getWxAccessById(id);
            bizResult.data.put("wxAccessDO", wxAccessDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query WxAccess error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = wxAccessDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<WxAccessDO> wxAccessList = wxAccessDAO.selectForPage(baseQuery);
            bizResult.data.put("wxAccessList", wxAccessList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view WxAccess list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            wxAccessDAO.delWxAccessById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete wxAccess error", e);
        }
        return bizResult;
    }

    public BizResult create(WxAccessDO wxAccessDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = wxAccessDAO.insert(wxAccessDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create WxAccess error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(WxAccessDO wxAccessDO) {
        BizResult bizResult = new BizResult();
        try {
            wxAccessDAO.updateByIdSelective(wxAccessDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update WxAccess error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult queryMySuite() {
        BizResult bizResult = new BizResult();
        Long accessId = AuthUser.getCurrentUser().getUserId();
        WxAccessDO wxAccessById = wxAccessDAO.getWxAccessById(accessId);
        if(wxAccessById == null){
            bizResult.code= CommonResultCode.PERM_ERROR;
            return bizResult;
        }
        List<SuiteView> suiteViewList = new ArrayList<SuiteView>();
        if(StringUtils.hasText(wxAccessById.getUnionid())){
            List<WxSuiteRelDO> wxSuiteRelByUnionId = wxSuiteRelDAO.getWxSuiteRelByUnionId(wxAccessById.getUnionid());

            for(WxSuiteRelDO wxSuiteRelDO:wxSuiteRelByUnionId){
                SuiteView suiteView =getSuiteView(wxSuiteRelDO.getSuiteId());
                if(suiteView == null){
                    continue;
                }
                suiteViewList.add(suiteView);
            }
        }else if(StringUtils.hasText(wxAccessById.getOpenid())){
            List<WxSuiteRelDO> wxSuiteRelByUnionId = wxSuiteRelDAO.getWxSuiteRelByOpenId(wxAccessById.getOpenid());
            for(WxSuiteRelDO wxSuiteRelDO:wxSuiteRelByUnionId){
                SuiteView suiteView =getSuiteView(wxSuiteRelDO.getSuiteId());
                if(suiteView == null){
                    continue;
                }
                suiteViewList.add(suiteView);
            }
        }
        bizResult.data.put("suiteViewList", suiteViewList);
        bizResult.success=true;

        return bizResult;
    }

    @Override
    public BizResult bindSuite(Long suiteId) {
        BizResult bizResult = new BizResult();
        Long accessId = AuthUser.getCurrentUser().getUserId();
        WxAccessDO wxAccessById = wxAccessDAO.getWxAccessById(accessId);
        if(wxAccessById == null){
            bizResult.code= CommonResultCode.PERM_ERROR;
            return bizResult;
        }
        if(StringUtils.hasText(wxAccessById.getUnionid())){
            WxSuiteRelQuery wxSuiteRelQuery = new WxSuiteRelQuery();
            wxSuiteRelQuery.setUnionid(wxAccessById.getUnionid());
            wxSuiteRelQuery.setSuiteId(suiteId);
            int count = wxSuiteRelDAO.countForPage(wxSuiteRelQuery);
            if(count>0){
                bizResult.code= CommonResultCode.SUITE_BIND_REPEAT;
                return bizResult;
            }


        }else if(StringUtils.hasText(wxAccessById.getOpenid())){
            WxSuiteRelQuery wxSuiteRelQuery = new WxSuiteRelQuery();
            wxSuiteRelQuery.setOpenid(wxAccessById.getOpenid());
            wxSuiteRelQuery.setSuiteId(suiteId);
            int count = wxSuiteRelDAO.countForPage(wxSuiteRelQuery);
            if(count>0){
                bizResult.code= CommonResultCode.SUITE_BIND_REPEAT;
                return bizResult;
            }
        }
        WxSuiteRelDO wxSuiteRelDO = new WxSuiteRelDO();
        wxSuiteRelDO.setUnionid(wxAccessById.getUnionid());
        wxSuiteRelDO.setOpenid(wxAccessById.getOpenid());
        wxSuiteRelDO.setSuiteId(suiteId);
        wxSuiteRelDAO.insert(wxSuiteRelDO);
        bizResult.success=true;
        return bizResult;
    }

    @Override
    public BizResult unBindSuite(Long suiteId) {
        BizResult bizResult = new BizResult();
        Long accessId = AuthUser.getCurrentUser().getUserId();
        WxAccessDO wxAccessById = wxAccessDAO.getWxAccessById(accessId);
        if(wxAccessById == null){
            bizResult.code= CommonResultCode.PERM_ERROR;
            return bizResult;
        }
        if(StringUtils.hasText(wxAccessById.getUnionid())){
            WxSuiteRelQuery wxSuiteRelQuery = new WxSuiteRelQuery();
            wxSuiteRelQuery.setUnionid(wxAccessById.getUnionid());
            wxSuiteRelQuery.setSuiteId(suiteId);
            List<WxSuiteRelDO> wxSuiteRelDOs = wxSuiteRelDAO.selectForPage(wxSuiteRelQuery);
            if(wxSuiteRelDOs.size() == 0){
                bizResult.success=true;
                return bizResult;
            }
            for(WxSuiteRelDO wxSuiteRelDO:wxSuiteRelDOs){
                wxSuiteRelDAO.delWxSuiteRelById(wxSuiteRelDO.getRelId());
            }



        }else if(StringUtils.hasText(wxAccessById.getOpenid())){
            WxSuiteRelQuery wxSuiteRelQuery = new WxSuiteRelQuery();
            wxSuiteRelQuery.setOpenid(wxAccessById.getOpenid());
            wxSuiteRelQuery.setSuiteId(suiteId);
            List<WxSuiteRelDO> wxSuiteRelDOs = wxSuiteRelDAO.selectForPage(wxSuiteRelQuery);
            if(wxSuiteRelDOs.size() == 0){
                bizResult.success=true;
                return bizResult;
            }
            for(WxSuiteRelDO wxSuiteRelDO:wxSuiteRelDOs){
                wxSuiteRelDAO.delWxSuiteRelById(wxSuiteRelDO.getRelId());
            }
        }
        bizResult.success=true;
        return bizResult;
    }

    private SuiteView getSuiteView(Long suiteId){
        if(suiteId == null){
            return null;
        }
        SuiteView suiteView = new SuiteView();
        SuiteDO suiteById = suiteDAO.getSuiteById(suiteId);
        if(suiteById == null){
            return null;
        }
        suiteView.setSuiteDO(suiteById);
        UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
        if(unitById == null){
            return null;
        }
        suiteView.setUnitDO(unitById);
        BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
        if(buildingById == null){
            return null;
        }
        suiteView.setBuildingDO(buildingById);
        VillageDO villageById = villageDAO.getVillageById(buildingById.getVillageId());
        if(villageById == null){
            return null;
        }
        suiteView.setVillageDO(villageById);
        return suiteView;
    }


}
